package com.workmanager.controller;

import com.alibaba.fastjson.JSONObject;
import com.workmanager.bean.StudentBean;
import com.workmanager.service.StudentMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

/**
 * Created by Administrator on 2016/9/4 0004.
 */
@RequestMapping("/information")
@Controller
public class MessageStudentController {
    @Autowired
    @Resource(name="infostudentService")
    private StudentMessageService infostudentService;

    @RequestMapping("/stuinfo")
    public String stumessagge(HttpServletRequest request, HttpServletResponse response){
        StudentBean astudent=(StudentBean) request.getSession().getAttribute("学生");//从session得到的学生Bean
        int studentid=Integer.valueOf(astudent.getStudent_id());
        //根据xueshengid得到学生的信息
        StudentBean student=infostudentService.getstudentmessage(studentid);
        System.out.println("----------------MessageStudentController");
        System.out.println(student.getStudent_interest()+"==="+student.getStudent_signature());
        request.getSession().setAttribute("STUDENT",student);
        List<String> classnames=infostudentService.getallclassname();
        request.getSession().setAttribute("classnames",classnames);
        if(student.getClass_id()!=null){
            String classname=infostudentService.getClassNamebyId(Integer.valueOf(student.getClass_id()));
            request.getSession().setAttribute("classname",classname);//把班级的名称保存在session当中
            System.out.println("MessageStudentController--------"+request.getSession().getAttribute("classname"));
        }else{
            request.getSession().setAttribute("classname","");
        }
        return "../student/messagechange.jsp";
    }
    //TODO:把修改后的信息添加到数据库当中,考虑session的期限性
    @RequestMapping("/changeinfo")
    public void changeinfo(HttpServletRequest request, HttpServletResponse response) throws IOException {

        StudentBean stu=(StudentBean)request.getSession().getAttribute("学生");
        String studentid=stu.getStudent_id();//得到学生的id

        StudentBean studentBean=new StudentBean();
        studentBean.setStudent_id(studentid);
        studentBean.setStudent_name(request.getParameter("xingming"));
        studentBean.setStudent_sex(request.getParameter("sex"));
        studentBean.setStudent_number(request.getParameter("xuehao"));
        String banji=request.getParameter("banji");//得到班级名称
        String class_id=infostudentService.getidfromclassname(banji);//根据班级名称得到班级的id
        studentBean.setClass_id(class_id);
        studentBean.setStudent_major(request.getParameter("major"));
        String phonenumber=request.getParameter("phonenumber");

        studentBean.setStudent_school(request.getParameter("school"));
        String birthday=request.getParameter("birthday");//得到的出生日期的格式是2016-09-08

            Date birth=java.sql.Date.valueOf(birthday);//把String转化为java.sql.Date

        studentBean.setStudent_brithday(birth);

        studentBean.setStudent_qq(request.getParameter("qqnumber"));
        studentBean.setStudent_wechat(request.getParameter("weixin"));
        studentBean.setStudent_address(request.getParameter("address"));
        studentBean.setStudent_interest(request.getParameter("interexts"));
        studentBean.setStudent_signature(request.getParameter("qianming"));

            studentBean.setStudent_phone(phonenumber);
        request.getSession().setAttribute("学生",studentBean);
            boolean updateresult=infostudentService.updateresult(studentBean);
            JSONObject json=new JSONObject();
        json.put("Whethersaved",updateresult);
        response.getWriter().print(json.toJSONString());


    }
    public StudentMessageService getStudentMessageService() {
        return infostudentService;
    }

    public void setStudentMessageService(StudentMessageService studentMessageService) {
        this.infostudentService = studentMessageService;
    }
}
